home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
JPNL Libraries
/
URL.java
< prev
Wrap
Text File
|
1996-05-23
|
3KB
|
143 lines
package au.com.peter.libraries;
import java.net.MalformedURLException;
public class URL {
public String protocol = null;
public String username = null;
public String password = null;
public String host = null;
public String path = null;
public int port = 0;
public static URL MakeURL( String url ) {
URL result;
try {
result = new URL( url );
} catch ( MalformedURLException e ) {
result = null;
}
return result;
}
public URL( String url ) throws MalformedURLException {
try {
int start, finish;
start = 0;
finish = url.length() - 1;
while ( url.charAt( start ) == ' ' ) {
start++;
}
while ( url.charAt( finish ) == ' ' ) {
finish--;
}
if ( url.charAt( start ) == '<' ) {
start++;
if ( url.charAt( finish ) == '>' ) {
finish--;
} else {
throw new MalformedURLException( "missing > in " + url );
}
}
if ( url.regionMatches( true, start, "url:", 0, 4 ) ) {
start += 4;
}
int saved_start = start;
start = url.indexOf( "://", start );
if ( start < 0 ) {
throw new MalformedURLException( "missing :// in " + url );
}
protocol = url.substring( saved_start, start - saved_start ).toLowerCase();
start += 3; // skip ://
saved_start = start;
start = url.indexOf( '/', start );
if ( start < saved_start ) {
start = -1;
}
path = url.substring( start+1, finish+1 );
host = url.substring( saved_start, start );
start = host.lastIndexOf( '@' );
if ( start >= 0 ) {
username = host.substring( 0, start );
host = host.substring( start + 1 );
start = username.lastIndexOf( ':' );
if ( start >= 0 ) {
password = username.substring( start + 1 );
username = username.substring( 0, start );
}
}
start = host.lastIndexOf( ':' );
if ( start >= 0 ) {
port = Integer.parseInt( host.substring( start + 1 ) );
host = host.substring( start + 1 );
} else {
port = ProtocolDefaultPort( protocol );
}
} catch ( MalformedURLException e ) {
throw e;
} catch ( Exception e ) {
throw new MalformedURLException( "malformed: " + url );
}
}
public void SetDefaultUsername( String email ) {
if ( username == null ) {
username = "anonymous";
}
if ( password == null ) {
password = email;
}
}
public static int ProtocolDefaultPort( String prot ) { // improve later
if ( prot.equals( "ftp" ) ) {
return 21;
} else if ( prot.equals( "http" ) ) {
return 80;
} else if ( prot.equals( "news" ) || prot.equals( "nntp" ) ) {
return 119;
} else if ( prot.equals( "smtp" ) ) {
return 25;
}
return 0;
}
public boolean IsAnonymous() {
if ( username == null || username.length() == 0 ) {
return true;
}
return username.equalsIgnoreCase( "ftp" ) || username.equalsIgnoreCase( "anonymous" );
}
public String toString( boolean secret ) {
String result = protocol + "://";
if ( !IsAnonymous() ) {
result += username;
if ( password != null ) {
result += ":";
if ( !secret ) {
result += password;
}
}
result += "@";
}
result += host;
if ( port == ProtocolDefaultPort( protocol ) ) {
result += ":" + port;
}
if ( path != null ) {
result += "/" + path;
}
return result;
}
public String toString() {
return toString( true );
}
}